package com.lyl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyl.common.R;
import com.lyl.dto.SetmealDto;
import com.lyl.entity.Category;
import com.lyl.entity.Setmeal;
import com.lyl.service.CategoryService;
import com.lyl.service.SetmealDishService;
import com.lyl.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 李亚龙
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    @CacheEvict(value = "setmealCache", allEntries = true)//删除"setmealCache"分类下的所有数据
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("保存到套餐菜品表：{}", setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("保存套餐成功");
    }

    /**
     * 套餐分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(int page, int pageSize, String name) {
        log.info("分页查询：page = {}, pageSize = {}, name = {}", page, pageSize, name);
        //构造分页构造器
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();

        //构造条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Setmeal::getName, name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        //执行查询
        setmealService.page(pageInfo, queryWrapper);

        BeanUtils.copyProperties(pageInfo, setmealDtoPage, "records");
        List<Setmeal> records = pageInfo.getRecords();

        List<SetmealDto> list = records.stream().map((setmeal) -> {
            //创建SetmealDto对象
            SetmealDto setmealDto = new SetmealDto();
            //获取setmeal中的categoryId
            Long categoryId = setmeal.getCategoryId();
            //通过categoryService服务层获取该对象
            Category category = categoryService.getById(categoryId);
            //通过该对象获取categoryName
            String categoryName = category.getName();
            //将setmeal的属性拷贝到setmealDto
            BeanUtils.copyProperties(setmeal, setmealDto);
            //最后设置setmealDto的categoryName属性值
            setmealDto.setCategoryName(categoryName);
            return setmealDto;
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(list);

        return R.success(setmealDtoPage);
    }


    /**
     * 批量删除套餐
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids) {
        log.info("删除套餐信息：{}", ids);
        setmealService.removeWithDish(ids);
        return R.success("套餐删除成功");
    }

    /**
     * 批量启售或者停售套餐，也可以单个停售启售
     *
     * @param s
     * @param ids
     * @return
     */
    @PostMapping("/status/{s}")
    public R<String> updateStatus(@PathVariable int s, @RequestParam List<Long> ids) {
        log.info("status = {}, ids = {}", s, ids);
        LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Setmeal::getStatus, s).in(Setmeal::getId, ids);
        setmealService.update(updateWrapper);
        return R.success(s == 0 ? "停售成功" : "启售成功");
    }

    /**
     * 根据CategoryId和Status查询套餐信息
     *
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        log.info("查询套餐列表：{}", setmeal);
        //构造条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}
